home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / vector.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  38.4 KB  |  1,288 lines

  1. #ifndef __STD_VECTOR__
  2. #define __STD_VECTOR__
  3. #pragma option push -b -a4 -Vx- -Ve- -w-inl -w-aus -w-sig
  4.  
  5. /***************************************************************************
  6.  *
  7.  * vector - declarations for the Standard Library vector class
  8.  *
  9.  * $Id: vector,v 1.75 1996/08/28 18:20:41 smithey Exp $
  10.  *
  11.  ***************************************************************************
  12.  *
  13.  * Copyright (c) 1994
  14.  * Hewlett-Packard Company
  15.  *
  16.  * Permission to use, copy, modify, distribute and sell this software
  17.  * and its documentation for any purpose is hereby granted without fee,
  18.  * provided that the above copyright notice appear in all copies and
  19.  * that both that copyright notice and this permission notice appear
  20.  * in supporting documentation.  Hewlett-Packard Company makes no
  21.  * representations about the suitability of this software for any
  22.  * purpose.  It is provided "as is" without express or implied warranty.
  23.  *
  24.  *
  25.  ***************************************************************************
  26.  *
  27.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  28.  * ALL RIGHTS RESERVED *
  29.  * The software and information contained herein are proprietary to, and
  30.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  31.  * intends to preserve as trade secrets such software and information.
  32.  * This software is furnished pursuant to a written license agreement and
  33.  * may be used, copied, transmitted, and stored only in accordance with
  34.  * the terms of such license and with the inclusion of the above copyright
  35.  * notice.  This software and information or any other copies thereof may
  36.  * not be provided or otherwise made available to any other person.
  37.  *
  38.  * Notwithstanding any other lease or license that may pertain to, or
  39.  * accompany the delivery of, this computer software and information, the
  40.  * rights of the Government regarding its use, reproduction and disclosure
  41.  * are as set forth in Section 52.227-19 of the FARS Computer
  42.  * Software-Restricted Rights clause.
  43.  * 
  44.  * Use, duplication, or disclosure by the Government is subject to
  45.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  46.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  47.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  48.  * P.O. Box 2328, Corvallis, Oregon 97339.
  49.  *
  50.  * This computer software and information is distributed with "restricted
  51.  * rights."  Use, duplication or disclosure is subject to restrictions as
  52.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  53.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  54.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  55.  * then the "Alternate III" clause applies.
  56.  *
  57.  **************************************************************************/
  58.  
  59. #include <stdcomp.h>
  60. #include "rw/stddefs.h"
  61.  
  62. #ifndef _RWSTD_HEADER_REQUIRES_HPP
  63. #include <algorithm>
  64. #include <iterator>
  65. #include <memory>
  66. #include <stdexcept>
  67. #else
  68. #include <algorithm.hpp>
  69. #include <iterator.hpp>
  70. #include <memory.hpp>
  71. #include <stdexcept.hpp>
  72. #endif
  73.  
  74. #ifndef vector
  75. #define vector vector
  76. #endif
  77.  
  78. #ifndef _RWSTD_NO_NAMESPACE
  79. namespace std {
  80. #endif
  81.  
  82. //
  83. // Note that _RWSTD_SIMPLE_DEFAULT(x)
  84. // will expand to: ' = x', or nothing,
  85. // depending on your compiler's capabilities and/or
  86. // flag settings (see stdcomp.h).
  87. //
  88. template <class T, class Allocator _RWSTD_SIMPLE_DEFAULT(allocator<T>) >
  89. class vector
  90. {
  91.   public:
  92.     //
  93.     // Types.
  94.     //
  95.     typedef T                                          value_type;
  96.     typedef Allocator                                  allocator_type;
  97.  
  98. private:
  99. #ifdef _RWSTD_ALLOCATOR
  100.     typedef _TYPENAME Allocator::rebind<T>::other value_alloc_type;
  101. #else
  102.     typedef allocator_interface<Allocator,T> value_alloc_type;
  103. #endif
  104.  
  105. public:
  106.  
  107. #ifndef _RWSTD_NO_COMPLICATED_TYPEDEF
  108.     typedef _TYPENAME _RWSTD_ALLOC_SIZE_TYPE             size_type;
  109.     typedef _TYPENAME _RWSTD_ALLOC_DIFF_TYPE             difference_type;
  110.     typedef _TYPENAME value_alloc_type::pointer          iterator;
  111.     typedef _TYPENAME value_alloc_type::const_pointer    const_iterator;
  112.     typedef _TYPENAME value_alloc_type::reference        reference;
  113.     typedef _TYPENAME value_alloc_type::const_reference  const_reference;
  114.   protected:
  115.     typedef _TYPENAME value_alloc_type::pointer        pointer;
  116.     typedef _TYPENAME value_alloc_type::const_pointer  const_pointer;
  117. #else
  118.     typedef size_t          size_type;
  119.     typedef ptrdiff_t       difference_type;
  120.     typedef T*              iterator;
  121.     typedef const T*        const_iterator;
  122.     typedef T&              reference;
  123.     typedef const T&        const_reference;
  124.   protected:
  125.     typedef T*              pointer;
  126.     typedef const T*        const_pointer;
  127. #endif  //_RWSTD_NO_COMPLICATED_TYPEDEF
  128.  
  129.   public:
  130.  
  131.     typedef _STD::reverse_iterator<const_iterator, value_type, const_reference, 
  132.                           const_pointer, difference_type>
  133.                                                        const_reverse_iterator;
  134.     typedef _STD::reverse_iterator<iterator, value_type,
  135.                reference, pointer, difference_type>
  136.                                                        reverse_iterator;
  137.   protected:
  138.  
  139.     size_type buffer_size;
  140.     allocator_type          the_allocator;
  141.     iterator start;
  142.     iterator finish;
  143.     iterator end_of_storage;
  144.  
  145.     void insert_aux (iterator position, const T& x);
  146.     void insert_aux (iterator position, size_type n, const T& x);
  147.  
  148.     void destroy(iterator start, iterator finish)
  149.     {
  150.        while ( start != finish)
  151.           value_alloc_type(the_allocator).destroy(start++);
  152.     }
  153.  
  154.    // 
  155.    //  Allocate buffers and fill with n values
  156.    //
  157.    void __init_n(size_type n, const T& value)
  158.     {
  159.        initialize(); 
  160.        start = value_alloc_type(the_allocator).allocate(n,0);
  161.        uninitialized_fill_n(start, n, value);
  162.        finish = start + n;
  163.        end_of_storage = finish;
  164.     } 
  165.  
  166.  
  167.   public:
  168.     //
  169.     // construct/copy/destroy
  170.     //
  171.     _EXPLICIT vector (const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator())) 
  172.           : start(0), finish(0), end_of_storage(0), the_allocator(alloc) 
  173.     { 
  174.        initialize(); 
  175.     }
  176.  
  177.     //
  178.     // Build a vector of size n with each element set to default for type T.
  179.     // This requires that T have a default constructor.
  180.     //
  181.     _EXPLICIT vector (size_type n, const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  182.           : the_allocator(alloc)
  183.     {
  184.         T value;
  185.         __init_n(n,value);
  186.     }
  187.     
  188.     
  189. #ifdef _RWSTD_NO_DEFAULT_TEMPLATE_ARGS
  190.     vector (void) 
  191.           : start(0), finish(0), end_of_storage(0), the_allocator(Allocator()) 
  192.     { ; }
  193.  
  194.     _EXPLICIT vector (size_type n)
  195.           : the_allocator(Allocator())
  196.     {
  197.         T value;
  198.         __init_n(n,value);
  199.     }
  200.  
  201.     vector (size_type n, const T& value) 
  202.           : the_allocator(Allocator())
  203.     {
  204.       __init_n(n,value);
  205.     }
  206. #endif
  207.  
  208.     vector (const vector<T,Allocator>& x)
  209.     {
  210.         initialize(); 
  211.         the_allocator = x.get_allocator();
  212.         start = value_alloc_type(the_allocator).allocate(x.end() - x.begin(),0);
  213.         finish = uninitialized_copy(x.begin(), x.end(), start);
  214.         end_of_storage = finish;
  215.     }
  216.  
  217.  
  218. #ifndef _RWSTD_NO_MEMBER_TEMPLATES
  219. template<class InputIterator>
  220.     vector (InputIterator first, InputIterator last,
  221.              const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  222.            : the_allocator(alloc)
  223.      {
  224.         size_type n;
  225.         initialize(); 
  226.         __initialize(n, size_type(0));
  227.         distance(first, last, n);
  228.         start = value_alloc_type(the_allocator).allocate(n,0);
  229.         finish = uninitialized_copy(first, last, start);
  230.         end_of_storage = finish;
  231.     }
  232.     vector (int n, const T& value,
  233.               const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  234.           : the_allocator(alloc)
  235.     { __init_n((size_type)n,value); }
  236.     vector (unsigned int n, const T& value,
  237.               const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  238.           : the_allocator(alloc)
  239.     { __init_n((size_type)n,value); }
  240.     vector (long n, const T& value,
  241.               const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  242.           : the_allocator(alloc)
  243.     { __init_n((size_type)n,value); }
  244.     vector (unsigned long n, const T& value,
  245.               const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  246.           : the_allocator(alloc)
  247.     { __init_n((size_type)n,value); }
  248.     vector (short n, const T& value,
  249.               const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  250.           : the_allocator(alloc)
  251.     { __init_n((size_type)n,value); }
  252.     vector (unsigned short n, const T& value,
  253.               const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  254.           : the_allocator(alloc)
  255.     { __init_n((size_type)n,value); }
  256.     vector (char n, const T& value,
  257.               const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  258.           : the_allocator(alloc)
  259.     { __init_n((size_type)n,value); }
  260.     vector (unsigned char n, const T& value,
  261.               const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  262.           : the_allocator(alloc)
  263.     { __init_n((size_type)n,value); }
  264. #ifndef _RWSTD_NO_BOOL
  265.     vector (bool n, const T& value,
  266.               const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  267.           : the_allocator(alloc)
  268.     { __init_n((size_type)n,value); }
  269. #endif
  270. #ifndef _RWSTD_NO_OVERLOAD_WCHAR
  271.     vector (wchar_t n, const T& value,
  272.               const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  273.           : the_allocator(alloc)
  274.     { __init_n((size_type)n,value); }
  275. #endif
  276. #else
  277.     //
  278.     // Build a vector of size n with each element set to copy of value.
  279.     //
  280.     vector (size_type n, const T& value, 
  281.                      const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  282.           : the_allocator(alloc)
  283.     {
  284.         __init_n(n,value);
  285.     }
  286.  
  287.     vector (const_iterator first, const_iterator last,
  288.              const Allocator& alloc _RWSTD_DEFAULT_ARG(Allocator()))
  289.            : the_allocator(alloc)
  290.      {
  291.         size_type n;
  292.         initialize(); 
  293.         __initialize(n, size_type(0));
  294.         distance(first, last, n);
  295.         start = value_alloc_type(the_allocator).allocate(n,0);
  296.         finish = uninitialized_copy(first, last, start);
  297.         end_of_storage = finish;
  298.     }
  299.  
  300. #ifdef _RWSTD_NO_DEFAULT_TEMPLATE_ARGS
  301.     vector (const_iterator first, const_iterator last)
  302.            : the_allocator(Allocator())
  303.      {
  304.         size_type n;
  305.         initialize(); 
  306.         __initialize(n, size_type(0));
  307.         distance(first, last, n);
  308.         start = value_alloc_type(the_allocator).allocate(n,0);
  309.         finish = uninitialized_copy(first, last, start);
  310.         end_of_storage = finish;
  311.     }
  312. #endif
  313. #endif
  314.     void initialize() 
  315.     {
  316.         buffer_size = 
  317.            max((size_type)1,__RWSTD::__rw_allocation_size((value_type*)0,(size_type)0));
  318.     }
  319.  
  320.     ~vector ()
  321.     { 
  322.         destroy(start, finish); 
  323.         value_alloc_type(the_allocator).deallocate(start,end_of_storage-start);
  324.     }
  325.     vector<T,Allocator>& operator= (const vector<T,Allocator>& x);
  326.  
  327. #ifndef _RWSTD_NO_MEMBER_TEMPLATES
  328.     template<class InputIterator> 
  329.     void assign (InputIterator first, InputIterator last)
  330. #else
  331.     void assign (const_iterator first, const_iterator last)
  332. #endif
  333.     {
  334.         erase(begin(), end()); insert(begin(), first, last);
  335.     }
  336.     //
  337.     // Assign n copies of default value of type T to vector.
  338.     // This requires that T have a default constructor.
  339.     //
  340. #ifndef _RWSTD_NO_MEMBER_TEMPLATES
  341.     template<class Size>
  342.     void assign (Size n)
  343. #else
  344.     void assign (size_type n)
  345. #endif
  346.     {
  347.         erase(begin(), end()); insert(begin(), n, T());
  348.     }
  349.     //
  350.     // Assign n copies of t to this vector.
  351.     //
  352. #ifndef _RWSTD_NO_MEMBER_TEMPLATES
  353.     template<class Size, class U>
  354.     void assign (Size n, const U& t)
  355. #else
  356.     void assign (size_type n, const T& t)
  357. #endif
  358.     {
  359.         erase(begin(), end()); insert(begin(), n, t);
  360.     }
  361.     allocator_type get_allocator() const
  362.     {
  363.         return the_allocator;
  364.     }
  365.  
  366.     //
  367.     // Iterators.
  368.     //
  369.     iterator       begin ()       { return start;  }
  370.     const_iterator begin () const { return start;  }
  371.     iterator       end ()         { return finish; }
  372.     const_iterator end ()   const { return finish; }
  373.  
  374.     reverse_iterator rbegin ()
  375.     { 
  376.         reverse_iterator tmp(end()); return tmp;
  377.     }
  378.     const_reverse_iterator rbegin () const
  379.     { 
  380.         const_reverse_iterator tmp(end()); return tmp;
  381.     }
  382.     reverse_iterator rend ()
  383.     { 
  384.         reverse_iterator tmp(begin()); return tmp;
  385.     }
  386.     const_reverse_iterator rend () const
  387.     { 
  388.         const_reverse_iterator tmp(begin()); return tmp;
  389.     }
  390.  
  391.     //
  392.     // Capacity.
  393.     //
  394.     size_type size ()     const { return size_type(end() - begin()); }
  395.     size_type max_size () const { return value_alloc_type(the_allocator).max_size();   }
  396.     void resize (size_type new_size);
  397.     void resize (size_type new_size, T value);
  398.  
  399.     size_type capacity () const { return size_type(end_of_storage - begin()); }
  400.     bool      empty ()    const { return begin() == end();                    }
  401.     void reserve (size_type n)
  402.     {
  403.         if (capacity() < n)
  404.         {
  405.             value_alloc_type va(the_allocator);
  406.             iterator tmp = va.allocate(n,start);
  407.             uninitialized_copy(begin(), end(), tmp);
  408.             destroy(start, finish);
  409.             va.deallocate(start,end_of_storage-start);
  410.             finish = tmp + size();
  411.             start = tmp;
  412.             end_of_storage = begin() + n;
  413.         }
  414.     }
  415.  
  416.     //
  417.     // Element access.
  418.     //
  419.     reference       operator[] (size_type n)       
  420.     {
  421. #ifdef _RWSTD_BOUNDS_CHECKING
  422.       _RWSTD_THROW(n >= size(), out_of_range, __RWSTD::rwse_OutOfRange);
  423.       return *(begin() + n);
  424. #else
  425.       return *(begin() + n);
  426. #endif
  427.     }
  428.   
  429.     const_reference operator[] (size_type n) const 
  430.     {
  431. #ifdef _RWSTD_BOUNDS_CHECKING
  432.       _RWSTD_THROW(n >= size(), out_of_range, __RWSTD::rwse_OutOfRange);
  433.       return *(begin() + n);
  434. #else
  435.       return *(begin() + n);
  436. #endif
  437.     }
  438.   
  439.       reference       at (size_type n)               
  440.     { 
  441.       _RWSTD_THROW(n >= size(), out_of_range, __RWSTD::rwse_OutOfRange);
  442.       return *(begin() + n); 
  443.     }
  444.     const_reference at (size_type n)         const 
  445.     { 
  446.       _RWSTD_THROW(n >= size(), out_of_range, __RWSTD::rwse_OutOfRange);
  447.       return *(begin() + n); 
  448.     }
  449.     reference       front ()                       { return *begin();       }
  450.     const_reference front ()                 const { return *begin();       }
  451.     reference       back ()                        { return *(end() - 1);   }
  452.     const_reference back ()                  const { return *(end() - 1);   }
  453.  
  454.     //
  455.     // Modifiers.
  456.     //
  457.     void push_back (const T& x)
  458.     {
  459.         if (finish != end_of_storage)
  460.         {
  461.             value_alloc_type(the_allocator).construct(finish, x); 
  462.             finish++;
  463.         }
  464.         else
  465.             insert_aux(end(), x);
  466.     }
  467.     void pop_back()
  468.     {
  469.         --finish; 
  470.         value_alloc_type(the_allocator).destroy(finish);
  471.     }
  472.     //
  473.     // Insert default value of type T at position.
  474.     // Requires that T have a default constructor.
  475.     //
  476.     iterator insert (iterator position)
  477.     {
  478.         size_type n = position - begin();
  479.         T x;
  480.         if (finish != end_of_storage && position == end())
  481.         {
  482.             value_alloc_type(the_allocator).construct(finish, x); finish++;
  483.         }
  484.         else
  485.             insert_aux(position, x);
  486.         return begin() + n;
  487.     }
  488.     //
  489.     // Insert x at position.
  490.     //
  491.     iterator insert (iterator position, const T& x)
  492.     {
  493.         size_type n = position - begin();
  494.         if (finish != end_of_storage && position == end())
  495.         {
  496.             value_alloc_type(the_allocator).construct(finish, x); finish++;
  497.         }
  498.         else
  499.             insert_aux(position, x);
  500.         return begin() + n;
  501.     }
  502.  
  503. #ifndef _RWSTD_NO_MEMBER_TEMPLATES
  504.     template<class InputIterator>
  505.     void insert (iterator position, InputIterator first, 
  506.                  InputIterator last);
  507.     void insert (iterator position, int n, const T& value)
  508.     { insert_aux(position,(size_type)n,value); }
  509.     void insert (iterator position, unsigned int n, const T& value)
  510.     { insert_aux(position,(size_type)n,value); }
  511.     void insert (iterator position, long n, T value)
  512.     { insert_aux(position,(size_type)n,value); }
  513.     void insert (iterator position, unsigned long n, const T& value)
  514.     { insert_aux(position,(size_type)n,value); }
  515.     void insert (iterator position, short n, const T& value)
  516.     { insert_aux(position,(size_type)n,value); }
  517.     void insert (iterator position, unsigned short n, const T& value)
  518.     { insert_aux(position,(size_type)n,value); }
  519.     void insert (iterator position, char n, const T& value)
  520.     { insert_aux(position,(size_type)n,value); }
  521.     void insert (iterator position, unsigned char n, const T& value)
  522.     { insert_aux(position,(size_type)n,value); }
  523. #ifndef _RWSTD_NO_OVERLOAD_WCHAR
  524.     void insert (iterator position, wchar_t n, const T& value)
  525.     { insert_aux(position,(size_type)n,value); }
  526. #endif
  527. #else
  528.     void insert (iterator position, size_type n, const T& x)
  529.     { insert_aux(position,n,x); }
  530.     void insert (iterator position, const_iterator first, const_iterator last);
  531. #endif
  532.  
  533.     void swap (vector<T,Allocator>& x)
  534.     {
  535.       if(the_allocator==x.the_allocator)
  536.        {
  537. #ifndef _RWSTD_NO_NAMESPACE
  538.         std::swap(start, x.start);
  539.         std::swap(finish, x.finish);
  540.         std::swap(end_of_storage, x.end_of_storage);
  541. #else
  542.         ::swap(start, x.start);
  543.         ::swap(finish, x.finish);
  544.         ::swap(end_of_storage, x.end_of_storage);
  545. #endif
  546.        }
  547.        else
  548.        {
  549.          vector<T,Allocator> _x = *this;
  550.          *this = x;
  551.          x=_x;
  552.        } 
  553.     }
  554.     iterator erase (iterator position)
  555.     {
  556.         if (position + 1 != end()) copy(position + 1, end(), position);
  557.         --finish;
  558.         value_alloc_type(the_allocator).destroy(finish);
  559.         return position;
  560.     }
  561.     iterator erase (iterator first, iterator last)
  562.     {
  563.         iterator i = copy(last, end(), first);
  564.         destroy(i, finish);
  565.         finish = finish - (last - first); 
  566.         return first;
  567.     }
  568.     void clear()
  569.     {
  570.         erase(begin(),end());
  571.     }
  572.  
  573. #ifndef _RWSTD_STRICT_ANSI
  574.     // Non-standard function for setting buffer allocation size
  575.     size_type allocation_size() { return buffer_size; }
  576.     size_type allocation_size(size_type new_size) 
  577.     { 
  578.        size_type tmp = buffer_size; 
  579.        buffer_size = max((size_type)1,new_size);
  580.        return tmp;
  581.     }
  582. #endif  
  583. };
  584.  
  585. template <class T, class Allocator>
  586. inline bool operator== (const vector<T,Allocator>& x, const vector<T,Allocator>& y)
  587. {
  588.     return x.size() == y.size() && equal(x.begin(), x.end(), y.begin());
  589. }
  590.  
  591. template <class T, class Allocator>
  592. inline bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y)
  593. {
  594.     return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
  595. }
  596.  
  597. #if !defined(_RWSTD_NO_NAMESPACE) || !defined(_RWSTD_NO_PART_SPEC_OVERLOAD)
  598. template <class T, class Allocator>
  599. inline bool operator!= (const vector<T,Allocator>& x, const vector<T,Allocator>& y)
  600. {
  601.     return !(x == y);
  602. }
  603.  
  604. template <class T, class Allocator>
  605. inline bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y)
  606. {
  607.     return y < x;
  608. }
  609.  
  610. template <class T, class Allocator>
  611. inline bool operator>= (const vector<T,Allocator>& x, const vector<T,Allocator>& y)
  612. {
  613.     return !(x < y);
  614. }
  615.  
  616. template <class T, class Allocator>
  617. inline bool operator<= (const vector<T,Allocator>& x, const vector<T,Allocator>& y)
  618. {
  619.     return !(y <  x);
  620. }
  621.  
  622. template <class T, class Allocator>
  623. inline void swap(vector<T,Allocator>& a, vector<T,Allocator>& b)
  624. {
  625.     a.swap(b);
  626. }
  627. #endif
  628.  
  629. #ifndef _RWSTD_NO_NAMESPACE
  630. }
  631. #endif
  632.  
  633. #ifdef _RWSTD_NO_TEMPLATE_REPOSITORY
  634. #include <vector.cc>
  635. #endif
  636.  
  637. #ifndef _RWSTD_NO_NAMESPACE
  638. namespace std {
  639. #endif
  640.  
  641. //
  642. // If bool is a builtin type, we provide a vector<bool,allocator> specialization.
  643. // We do not provide the allocator interface for this specialization.
  644. //
  645. #ifndef _RWSTD_NO_BOOL
  646.  
  647. #define __WORD_BIT (int(CHAR_BIT*sizeof(unsigned int)))
  648.  
  649. _RWSTD_TEMPLATE
  650. class _RWSTDExport vector<bool, allocator<bool> >
  651. {
  652.   public:
  653.     //
  654.     // types
  655.     //
  656.     typedef allocator<bool>                             allocator_type;
  657.     typedef bool                                        value_type;
  658.  
  659.   private:
  660. #ifdef _RWSTD_ALLOCATOR
  661.     typedef allocator<bool>::rebind<unsigned int>::other value_alloc_type;
  662. #else
  663.     typedef allocator_interface<allocator_type,unsigned int> value_alloc_type;
  664. #endif
  665.  
  666.   public:
  667. #ifdef _RWSTD_NO_EMBEDDED_TYPEDEF
  668.     typedef allocator<bool>::size_type               size_type;
  669.     typedef allocator<bool>::difference_type         difference_type;
  670. #else
  671.     typedef allocator_type::size_type          size_type;
  672.     typedef allocator_type::difference_type    difference_type;
  673. #endif
  674.  
  675.   protected:
  676.     typedef value_alloc_type::pointer       pointer;
  677.     typedef value_alloc_type::const_pointer const_pointer;
  678.  
  679.   public:
  680.  
  681.     //
  682.     // forward declarations
  683.     //
  684.     class iterator;
  685.     class const_iterator;
  686.  
  687.     //
  688.     // bit reference
  689.     //
  690.     class reference
  691.     {
  692.         friend class iterator;
  693.         friend class const_iterator;
  694.       protected:
  695.         unsigned int* p;
  696.         unsigned int mask;
  697.         reference (unsigned int* x, unsigned int y) : p(x), mask(y) {}
  698.       public:
  699.         reference () : p(0), mask(0) {}
  700.         operator bool () const { return !(!(*p & mask)); }
  701.         reference& operator= (bool x)
  702.         {
  703.             if (x)      
  704.                 *p |= mask;
  705.             else
  706.                 *p &= ~mask;
  707.             return *this;
  708.         }
  709.         reference& operator= (const reference& x) { return *this = bool(x); }
  710.         bool operator== (const reference& x) const
  711.         {
  712.             return bool(*this) == bool(x);
  713.         }
  714.         bool operator< (const reference& x) const
  715.         {
  716. #ifndef _MSC_VER
  717.             return bool(*this) < bool(x);
  718. #else
  719.             return int(*this) < int(x);
  720. #endif /* _MSC_VER */
  721.         }
  722.         bool operator!= (const reference& x) const
  723.         {
  724.             return !(*this == x);
  725.         }
  726.         bool operator> (const reference& x) const
  727.         {
  728.             return  x < *this;
  729.         }
  730.          bool operator>= (const reference& x) const
  731.         {
  732.             return !(*this < x);
  733.         }
  734.         bool operator<= (const reference& x) const
  735.         {
  736.             return !(*this > x);
  737.         }
  738.         void flip () { *p ^= mask; }
  739.     };
  740.     
  741.     typedef bool const_reference;
  742.     //
  743.     // Definition of our iterator.
  744.     //
  745.     class iterator : public random_access_iterator<bool, difference_type>
  746.     {
  747.         friend class vector<bool,allocator<bool> >;
  748.         friend class const_iterator;
  749.  
  750.       protected:
  751.  
  752.         unsigned int* p;
  753.         unsigned int  offset;
  754.  
  755.         void bump_up ()
  756.         {
  757.             if (offset++ == __WORD_BIT - 1)
  758.             {
  759.                 offset = 0; ++p;
  760.             }
  761.         }
  762.         void bump_down ()
  763.         {
  764.             if (offset-- == 0)
  765.             {
  766.                 offset = __WORD_BIT - 1; --p;
  767.             }
  768.         }
  769.  
  770.       public:
  771.         iterator () : p(0), offset(0) {}
  772.         iterator (unsigned int* x, unsigned int y) : p(x), offset(y) {}
  773.  
  774.         reference operator* () const { return reference(p, 1U << offset); }
  775.         iterator& operator++ ()
  776.         {
  777.             bump_up(); return *this;
  778.         }
  779.         iterator operator++ (int)
  780.         {
  781.             iterator tmp = *this; bump_up(); return tmp;
  782.         }
  783.         iterator& operator-- ()
  784.         {
  785.             bump_down(); return *this;
  786.         }
  787.         iterator operator-- (int)
  788.         {
  789.             iterator tmp = *this; bump_down(); return tmp;
  790.         }
  791.         iterator& operator+= (difference_type i)
  792.         {
  793.             difference_type n = i + offset;
  794.             p += n / __WORD_BIT;
  795.             n = n % __WORD_BIT;
  796.             if (n < 0)
  797.             {
  798.                 offset = n + __WORD_BIT; --p;
  799.             }
  800.             else
  801.                 offset = n;
  802.             return *this;
  803.         }
  804.         iterator& operator-= (difference_type i)
  805.         {
  806.             *this += -i; return *this;
  807.         }
  808.         iterator operator+ (difference_type i) const
  809.         {
  810.             iterator tmp = *this; return tmp += i;
  811.         }
  812.         iterator operator- (difference_type i) const
  813.         {
  814.             iterator tmp = *this; return tmp -= i;
  815.         }
  816.         difference_type operator- (iterator x) const
  817.         {
  818.             return __WORD_BIT * (p - x.p) + offset - x.offset;
  819.         }
  820.         reference operator[] (difference_type i)
  821.         {
  822.             return *(*this + i);
  823.         }
  824.         bool operator== (const iterator& x) const
  825.         {
  826.             return p == x.p && offset == x.offset;
  827.         }
  828.         bool operator< (const iterator& x) const
  829.         {
  830.             return p < x.p || (p == x.p && offset < x.offset);
  831.         }
  832.         bool operator!= (const iterator& x) const
  833.         {
  834.             return !(*this == x);
  835.         }
  836.         bool operator> (const iterator& x) const
  837.         {
  838.             return x < *this;
  839.         }
  840.          bool operator>= (const iterator& x) const
  841.         {
  842.             return !(*this < x);
  843.         }
  844.         bool operator<= (const iterator& x) const
  845.         {
  846.             return !(*this > x);
  847.         }
  848.     };
  849.     //
  850.     // Definition of our const_iterator.
  851.     //
  852.     class const_iterator
  853.         : public random_access_iterator<bool, difference_type>
  854.     {
  855.         friend class vector<bool,allocator<bool> >;
  856.  
  857.       protected:
  858.  
  859.         unsigned int* p;
  860.         unsigned int offset;
  861.         void bump_up ()
  862.         {
  863.             if (offset++ == __WORD_BIT - 1)
  864.             {
  865.                 offset = 0; ++p;
  866.             }
  867.         }
  868.         void bump_down()
  869.         {
  870.             if (offset-- == 0)
  871.             {
  872.                 offset = __WORD_BIT - 1; --p;
  873.             }
  874.         }
  875.  
  876.       public:
  877.         const_iterator () : p(0), offset(0) {}
  878.         const_iterator (unsigned int* x, unsigned int y) : p(x), offset(y) {}
  879. #ifndef _MSC_VER
  880.         const_iterator (const vector<bool,allocator<bool> >::iterator& x) : p(x.p), offset(x.offset) {}
  881. #else
  882.         const_iterator (const iterator& x) : p(x.p), offset(x.offset) {}
  883. #endif
  884.         const_reference operator* () const
  885.         {
  886.             return reference(p, 1U << offset);
  887.         }
  888.         const_iterator& operator++ ()
  889.         {
  890.             bump_up(); return *this;
  891.         }
  892.         const_iterator operator++ (int)
  893.         {
  894.             const_iterator tmp = *this; bump_up(); return tmp;
  895.         }
  896.         const_iterator& operator-- ()
  897.         {
  898.             bump_down(); return *this;
  899.         }
  900.         const_iterator operator-- (int)
  901.         {
  902.             const_iterator tmp = *this; bump_down(); return tmp;
  903.         }
  904.         const_iterator& operator+= (difference_type i)
  905.         {
  906.             difference_type n = i + offset;
  907.             p += n / __WORD_BIT;
  908.             n = n % __WORD_BIT;
  909.             if (n < 0)
  910.             {
  911.                 offset = n + __WORD_BIT; --p;
  912.             }
  913.             else
  914.                 offset = n;
  915.             return *this;
  916.         }
  917.         const_iterator& operator-= (difference_type i)
  918.         {
  919.             *this += -i; return *this;
  920.         }
  921.         const_iterator operator+ (difference_type i) const
  922.         {
  923.             const_iterator tmp = *this; return tmp += i;
  924.         }
  925.         const_iterator operator- (difference_type i) const
  926.         {
  927.             const_iterator tmp = *this; return tmp -= i;
  928.         }
  929.         difference_type operator- (const_iterator x) const
  930.         {
  931.             return __WORD_BIT * (p - x.p) + offset - x.offset;
  932.         }
  933.         const_reference operator[] (difference_type i)
  934.         { 
  935.             return *(*this + i); 
  936.         }
  937.         bool operator== (const const_iterator& x) const
  938.         {
  939.             return p == x.p && offset == x.offset;
  940.         }
  941.         bool operator< (const const_iterator& x) const
  942.         {
  943.             return p < x.p || (p == x.p && offset < x.offset);
  944.         }
  945.         bool operator!= (const const_iterator& x) const
  946.         {
  947.             return !(*this == x);
  948.         }
  949.         bool operator> (const const_iterator& x) const
  950.         {
  951.             return x < *this;
  952.         }
  953.          bool operator>= (const const_iterator& x) const
  954.         {
  955.             return !(*this < x);
  956.         }
  957.         bool operator<= (const const_iterator& x) const
  958.         {
  959.             return !(*this > x);
  960.         }
  961.     };
  962.     //
  963.     // types
  964.     //
  965.     typedef _STD::reverse_iterator<const_iterator,
  966.                              value_type,
  967.                              const_reference, const_pointer,
  968.                              difference_type> const_reverse_iterator;
  969.     typedef _STD::reverse_iterator<iterator,
  970.                              value_type,
  971.                              reference, pointer,
  972.                              difference_type> reverse_iterator;
  973.   protected:
  974.  
  975.     allocator_type          the_allocator;
  976.     iterator                start;
  977.     iterator                finish;
  978.     unsigned int*           end_of_storage;
  979.  
  980.     unsigned int* bit_alloc (size_type n)
  981.     {
  982.         return value_alloc_type(the_allocator).allocate((n + __WORD_BIT - 1)/__WORD_BIT,start.p);
  983.     }
  984.     void initialize (size_type n)
  985.     {
  986.         unsigned int* q = bit_alloc(n);
  987.         end_of_storage = q + (n + __WORD_BIT - 1)/__WORD_BIT;
  988.         start = iterator(q, 0);
  989.         finish = start + n;
  990.     }
  991.     void insert_aux (iterator position, bool x);
  992.  
  993.   public:
  994.  
  995.     //
  996.     // construct/copy/destroy
  997.     //
  998.     vector ()
  999.         : start(iterator()), finish(iterator()), end_of_storage(0) {}
  1000.     _EXPLICIT vector (size_type n, bool value = bool())
  1001.     {
  1002.         initialize(n); 
  1003. #ifdef _RWSTD_FILL_NAME_CLASH
  1004.         std_fill(start.p, end_of_storage, value ? ~0 : 0);
  1005. #else
  1006.         fill(start.p, end_of_storage, value ? ~0 : 0);
  1007. #endif
  1008.     }
  1009.     vector (const vector<bool,allocator<bool> >& x)
  1010.     {
  1011.         initialize(x.size()); copy(x.begin(), x.end(), start);
  1012.     }
  1013.  
  1014. #ifndef _RWSTD_NO_MEMBER_TEMPLATES
  1015. template<class InputIterator>
  1016.     vector (InputIterator first, InputIterator last)
  1017.     {
  1018.         size_type n;
  1019.         __initialize(n, size_type(0));
  1020.         distance(first, last, n);
  1021.         initialize(n);
  1022.         copy(first, last, start);
  1023.     }
  1024. #else
  1025.     vector (const_iterator first, const_iterator last)
  1026.     {
  1027.         size_type n;
  1028.         __initialize(n, size_type(0));
  1029.         distance(first, last, n);
  1030.         initialize(n);
  1031.         copy(first, last, start);
  1032.     }
  1033.     vector (const bool* first, const bool* last)
  1034.     {
  1035.         size_type n;
  1036.         __initialize(n, size_type(0));
  1037.         distance(first, last, n);
  1038.         initialize(n);
  1039.         copy(first, last, start);
  1040.     }
  1041. #endif
  1042.     ~vector () {
  1043.      value_alloc_type(the_allocator).deallocate(start.p,
  1044.                   end_of_storage-start.p); 
  1045.      }
  1046.     vector& operator= (const vector<bool,allocator<bool> >& x)
  1047.     {
  1048.         if (&x == this) return *this;
  1049.         if (x.size() > capacity())
  1050.         {
  1051.             value_alloc_type(the_allocator).deallocate(start.p,
  1052.                              end_of_storage-start.p); 
  1053.             initialize(x.size());
  1054.         }
  1055.         copy(x.begin(), x.end(), begin());
  1056.         finish = begin() + x.size();
  1057.         return *this;
  1058.     }
  1059. #ifndef _RWSTD_NO_MEMBER_TEMPLATES
  1060.     template<class InputIterator>
  1061.     void assign (InputIterator first, InputIterator last)
  1062. #else
  1063.     void assign (const_iterator first, const_iterator last)
  1064. #endif
  1065.     {
  1066.         erase(begin(), end()); insert(begin(), first, last);
  1067.     }
  1068.  
  1069. #ifndef _RWSTD_NO_MEMBER_TEMPLATES
  1070.     template<class Size>
  1071.     void assign (Size n, const bool& t = bool())
  1072. #else
  1073.     void assign (size_type n, const bool& t = bool())
  1074. #endif
  1075.     {
  1076.         erase(begin(), end()); insert(begin(), n, t);
  1077.     }
  1078.     allocator_type get_allocator() const
  1079.     {
  1080.         return the_allocator;
  1081.     }
  1082.  
  1083.     //
  1084.     // iterators
  1085.     //
  1086.     iterator       begin ()       { return start; }
  1087.     const_iterator begin () const 
  1088.     { return const_iterator(start.p,start.offset); }
  1089.     iterator       end   ()       { return finish; }
  1090.     const_iterator end   () const 
  1091.     { return const_iterator(finish.p,finish.offset); }
  1092.  
  1093.     reverse_iterator       rbegin () { return reverse_iterator(end()); }
  1094.     const_reverse_iterator rbegin () const
  1095.     { 
  1096.         return const_reverse_iterator(end()); 
  1097.     }
  1098.     reverse_iterator       rend () { return reverse_iterator(begin()); }
  1099.     const_reverse_iterator rend () const
  1100.     {
  1101.         return const_reverse_iterator(begin()); 
  1102.     }
  1103.  
  1104.     //
  1105.     // capacity
  1106.     //
  1107.     size_type size     () const { return size_type(end() - begin());  }
  1108.     size_type max_size () const { return value_alloc_type(the_allocator).max_size(); }
  1109.     void resize (size_type new_size, bool c = false);
  1110.     size_type capacity () const
  1111.     {
  1112.         return size_type(const_iterator(end_of_storage, 0) - begin());
  1113.     }
  1114.     bool empty () const { return begin() == end(); }
  1115.     void reserve (size_type n)
  1116.     {
  1117.         if (capacity() < n)
  1118.         {
  1119.             unsigned int* q = bit_alloc(n);
  1120.             finish = copy(begin(), end(), iterator(q, 0));
  1121.             value_alloc_type(the_allocator).deallocate(start.p,
  1122.                        end_of_storage-start.p);
  1123.             start = iterator(q, 0);
  1124.             end_of_storage = q + (n + __WORD_BIT - 1)/__WORD_BIT;
  1125.         }
  1126.     }
  1127.  
  1128.     //
  1129.     // element access
  1130.     //
  1131.     reference       operator[] (size_type n)       { return *(begin() + n); }
  1132.     const_reference operator[] (size_type n) const { return *(begin() + n); }
  1133.     reference       at (size_type n)               
  1134.     { 
  1135.       _RWSTD_THROW(n >= size(), out_of_range, __RWSTD::rwse_OutOfRange);
  1136.       return *(begin() + n); 
  1137.     }
  1138.     const_reference at (size_type n)         const 
  1139.     {
  1140.       _RWSTD_THROW(n >= size(), out_of_range, __RWSTD::rwse_OutOfRange);
  1141.       return *(begin() + n); 
  1142.     }
  1143.     reference       front ()       { return *begin();     }
  1144.     const_reference front () const { return *begin();     }
  1145.     reference       back  ()       { return *(end() - 1); }
  1146.     const_reference back  () const { return *(end() - 1); }
  1147.  
  1148.     //
  1149.     // modifiers
  1150.     //
  1151.     void push_back (const bool& x)
  1152.     {
  1153.         if (finish.p != end_of_storage)
  1154.             *finish++ = x;
  1155.         else
  1156.             insert_aux(end(), x);
  1157.     }
  1158.     void pop_back () { --finish; }
  1159.     iterator insert (iterator position, const bool& x = bool())
  1160.     {
  1161.         size_type n = position - begin();
  1162.         if (finish.p != end_of_storage && position == end())
  1163.             *finish++ = x;
  1164.         else
  1165.             insert_aux(position, x);
  1166.         return begin() + n;
  1167.     }
  1168.     void insert (iterator position, size_type n, const bool& x);
  1169.  
  1170. #ifndef _RWSTD_NO_MEMBER_TEMPLATES
  1171.     template<class InputIterator>
  1172.     void insert (iterator position, InputIterator first, InputIterator last);
  1173. #else
  1174.     void insert (iterator position, const_iterator first, 
  1175.                  const_iterator last);
  1176. #endif
  1177.  
  1178.     iterator erase (iterator position)
  1179.     {
  1180. #ifndef _RWSTD_NO_NAMESPACE
  1181.         using namespace rel_ops;
  1182. #endif
  1183.         if (position + 1 != end())
  1184.             copy(position + 1, end(), position);
  1185.         --finish;
  1186.         return position;
  1187.     }
  1188.     iterator erase(iterator first, iterator last)
  1189.     {
  1190.         finish = copy(last, end(), first);
  1191.         return first;
  1192.     }
  1193.     void swap (vector<bool,allocator<bool> >& x)
  1194.     {
  1195. #ifndef _RWSTD_NO_NAMESPACE
  1196.         std::swap(start,          x.start);
  1197.         std::swap(finish,         x.finish);
  1198.         std::swap(the_allocator,  x.the_allocator);
  1199.         std::swap(end_of_storage, x.end_of_storage);
  1200. #else
  1201.         ::swap(start,          x.start); 
  1202.         ::swap(finish,         x.finish);
  1203.         ::swap(the_allocator,  x.the_allocator);
  1204.         ::swap(end_of_storage, x.end_of_storage);
  1205. #endif
  1206.     }
  1207.     static void swap(reference x, reference y);
  1208.     void flip ();
  1209.     void clear()
  1210.     {
  1211.         erase(begin(),end());
  1212.     }
  1213. };
  1214.  
  1215. _RWSTD_TEMPLATE
  1216. inline bool operator== (const vector<bool,allocator<bool> >& x, 
  1217.                         const vector<bool,allocator<bool> >& y)
  1218. {
  1219.     return x.size() == y.size() && equal(x.begin(), x.end(), y.begin());
  1220. }
  1221.  
  1222. _RWSTD_TEMPLATE
  1223. inline bool operator< (const vector<bool,allocator<bool> >& x, 
  1224.                        const vector<bool,allocator<bool> >& y)
  1225. {
  1226.      vector<bool,allocator<bool> >::const_iterator first1 = x.begin(), 
  1227.                                            last1 =x.end(),
  1228.                                            first2 = y.begin(),
  1229.                                            last2 = y.end();
  1230.  
  1231.     while (first1 != last1 && first2 != last2)
  1232.     {
  1233.         if ((int)*first1 < (int)*first2)     return true;
  1234.         if ((int)*first2++ < (int)*first1++) return false;
  1235.     }
  1236.     return first1 == last1 && first2 != last2;
  1237. }
  1238.  
  1239. #if !defined(_RWSTD_NO_NAMESPACE) || !defined(_RWSTD_NO_PART_SPEC_OVERLOAD)
  1240. _RWSTD_TEMPLATE
  1241. inline bool operator!= (const vector<bool,allocator<bool> >& x,
  1242.                         const vector<bool,allocator<bool> >& y)
  1243. {
  1244.     return !(x == y);
  1245. }
  1246.  
  1247. _RWSTD_TEMPLATE
  1248. inline bool operator> (const vector<bool,allocator<bool> >& x, 
  1249.                        const vector<bool,allocator<bool> >& y)
  1250. {
  1251.     return y < x;
  1252. }
  1253.  
  1254. _RWSTD_TEMPLATE
  1255. inline bool operator>= (const vector<bool,allocator<bool> >& x, 
  1256.                         const vector<bool,allocator<bool> >& y)
  1257. {
  1258.     return !(x < y);
  1259. }
  1260.  
  1261. _RWSTD_TEMPLATE
  1262. inline bool operator<= (const vector<bool,allocator<bool> >& x, 
  1263.                         const vector<bool,allocator<bool> >& y)
  1264. {
  1265.     return !(y <  x);
  1266. }
  1267.  
  1268. _RWSTD_TEMPLATE
  1269. inline void swap(vector<bool,allocator<bool> >& a, vector<bool,allocator<bool> >& b)
  1270. {
  1271.     a.swap(b);
  1272. }
  1273. #endif
  1274.  
  1275. #undef __WORD_BIT
  1276.  
  1277. #endif /*_RWSTD_NO_BOOL*/
  1278.  
  1279. #ifndef _RWSTD_NO_NAMESPACE
  1280. }
  1281. #endif
  1282.  
  1283. #undef vector
  1284.  
  1285. #pragma option pop
  1286.  
  1287. #endif /*__STD_VECTOR__*/
  1288.